//
//  QDMPStationTableViewCell.m
//  QDMetroClient
//
//  Created by YueWen on 2019/2/14.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDMPStationTableViewCell.h"
#import "QDMPStationCollectionViewCell.h"

NSInteger QDMPStationTableViewItemHeight = 86;
NSInteger QDMPStationTableViewTopHeight = 95;

CGFloat QDMPStationTableViewCellHeight(NSInteger count,BOOL isShort,BOOL isOnly) {
    
    CGFloat top = QDMPStationTableViewTopHeight;
    CGFloat insetTop = 14;
    CGFloat insetBottom = 15;
    
    //顶部标题高度 + inset的top and bottom, and ItemHeight
//    CGFloat height = top + insetTop + insetBottom + QDMPStationTableViewItemHeight;;
    
//    if (isOnly) { return height; }// 只有一个，不再包含查看更多、收起按钮
    
    //如果是缩短状态下的
//    if (isShort) { return height + 40 + 29; }
    
    //如果是展开的高度
    return top + insetTop + insetBottom + QDMPStationTableViewItemHeight * count + 28 * (count - 1) + 5 + 10;
}


@interface QDMPStationTableViewCell()

/// 底部的阴影视图
@property (nonatomic, strong) UIImageView *contentImageView;

/// 上箭头图片
@property (nonatomic, strong) UIImage *upImage;
/// 下箭头图片
@property (nonatomic, strong) UIImage *downImage;
/// 顶部点击的control
@property (nonatomic, strong) UIControl *headerTouchControl;

@end

@implementation QDMPStationTableViewCell

- (void)buildView {
    [super buildView];
    
    UIImage *image = @"mp_st_arrow_right".ritl_image;
    self.upImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationLeft];//上箭头
    self.downImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationRight];
    
    self.contentView.backgroundColor = QDSMainPageBottomColor;
    
    self.topLineView.backgroundColor = RITLColorFromIntRBG(237, 240, 238);
    self.topLineInset = UIEdgeInsetsMake(91, 38, 0, 38);
    self.topLineHeight = 0.5;
    
    self.cycleView.autoScroll = false;
    self.cycleView.infiniteLoop = false;
    self.cycleView.contentView.scrollEnabled = false;
    
    self.stationLabel = ({
        
        UILabel *label = [UILabel new];
        label.backgroundColor = QDSMainPageBottomColor;
        label.text = @"苗岭路";
        label.font = RITLSemiboldFont(22);
        label.textColor = QDSColorDefaultBlack;
        
        label;
    });
    
    self.contStationLabel = ({
        
        UILabel *label = [UILabel new];
        label.backgroundColor = QDSMainPageBottomColor;
        label.text = @"(附近站点)";
        label.font = RITLRegularFont(14);
        label.textColor = QDSColorDefaultBlack;
        
        label;
    });
    
    
    self.countLabel = ({
        
        UILabel *label = [UILabel new];
        label.backgroundColor = QDSMainPageBottomColor;
        label.text = @"3条线路经过此站";
        label.font = RITLLightFont(14);
        label.textColor = RITLColorSimpleFromIntRBG(179);
        
        label;
    });
    
    self.distanceLabel = ({
        
        UILabel *label = [UILabel new];
        label.backgroundColor = QDSMainPageBottomColor;
        label.text = @"距离约600米";
        label.font = RITLRegularFont(14);
        label.textColor = RITLColorSimpleFromIntRBG(179);
        
        label;
    });
    
    self.contentImageView = ({
        
        UIImageView *imageView = [UIImageView new];
        imageView.backgroundColor = QDSMainPageBottomColor;
        imageView.userInteractionEnabled = true;
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.image = [@"gen_background".ritl_image resizableImageWithCapInsets:UIEdgeInsetsMake(17, 17, 17, 17)];
        
        imageView;
    });
    
    self.moreButton = ({
        
        UIButton *view = [UIButton new];
        view.adjustsImageWhenHighlighted = false;
        view.backgroundColor = RITLColorFromIntRBG(245, 247, 245);
        
        view.titleLabel.font = RITLRegularFont(12);
        
        [view setTitle:@"查看更多" forState:UIControlStateNormal];
        [view setTitle:@"收起" forState:UIControlStateSelected];
        
        [view setTitleColor:QDSColorDefaultGray2 forState:UIControlStateNormal];
        [view setTitleColor:QDSColorDefaultGray2 forState:UIControlStateSelected];
        
//        [view setImage:self.upImage forState:UIControlStateNormal];
//        [view setImage:self.downImage forState:UIControlStateSelected];
        
        view;
    });
    
    self.headerTouchControl = ({
       
        __weak typeof(self) weakSelf = self;
        
        UIControl *touchControl = UIControl.new;
        touchControl.backgroundColor = UIColor.clearColor;
        [touchControl controlEvents:UIControlEventTouchUpInside handle:^(__kindof UIControl * _Nonnull sender) {
            if (weakSelf.selectHandler) {
               weakSelf.selectHandler(weakSelf.cycleView, 0);//模拟点击回调
            }
        }];
        
        touchControl;
    });
    
    [self.contentView addSubview:self.contentImageView];
    [self.contentImageView addSubview:self.headerTouchControl];
    [self.contentImageView addSubview:self.stationLabel];
    [self.contentImageView addSubview:self.contStationLabel];
    [self.contentImageView addSubview:self.countLabel];
    [self.contentImageView addSubview:self.distanceLabel];
//    [self.contentImageView addSubview:self.moreButton];
    
    [self.headerTouchControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(15);
        make.left.offset(10);
        make.height.mas_equalTo(90 - 15);
        make.right.inset(10);
    }];
    
    [self.stationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(isSmallerScreen() ? 25 : 29);
        make.top.offset(29);
        make.height.mas_equalTo(30);
    }];
    
    [self.contStationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.stationLabel.mas_right).offset(4);
        make.top.offset(37);
        make.height.mas_equalTo(20);
    }];
    
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.stationLabel);
        make.top.offset(59);
        make.height.mas_equalTo(20);
    }];
    
    [self.distanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(20);
        make.top.offset(59);
        make.height.mas_equalTo(20);
    }];
    
    [self.contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(11);
        make.right.inset(11);
        make.top.bottom.offset(0);
    }];
    
    
    
//    [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.offset(29);
//        make.right.bottom.inset(29);
//        make.height.mas_equalTo(40);
//    }];
//
    // 重置滚动视图
    [self.cycleView removeFromSuperview];
    [self.contentImageView addSubview:self.cycleView];
    
    self.cycleView.contentView.backgroundColor = UIColor.whiteColor;
    [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(9);
        make.right.inset(9);
        make.top.offset(QDMPStationTableViewTopHeight);
        make.bottom.inset(15).priorityHigh();
     }];
    
    [self.contentView bringSubviewToFront:self.topLineView];
}


/// 重置滚动视图
- (void)makeCycleViewConstraints:(RITLSDCycleScrollView *)view {}



#pragma mark - <RITLSDCycleScrollViewDataSource>

- (Class)cycleViewCustomCollectionViewCellClass:(RITLSDCycleScrollView *)cycleView {
    return QDMPStationCollectionViewCell.class;
}


- (UICollectionViewLayout *)cycleScollViewcustomCollectionViewLayout:(RITLSDCycleScrollView *)cycleView {
    
    UICollectionViewFlowLayout *flowLayout = UICollectionViewFlowLayout.new;
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.sectionInset = UIEdgeInsetsMake(14, 0, 0, 0);
    flowLayout.itemSize = CGSizeMake(RITL_SCREEN_WIDTH -  (/*isSmallerScreen() ? 14 : */20) * 2, QDMPStationTableViewItemHeight);
    flowLayout.minimumLineSpacing = 28;
    flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
    
    return flowLayout;
}


//- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
//    //拒绝父类的点击block回调
//}


@end
